
var express = require('express');

var app = express();

var secret = require('./config/credentials');

var logger = require('morgan');
var fs = require('fs')
var url = require("url");
var querystring = require("querystring");

var accessLogStream = fs.createWriteStream(__dirname + '/access.log', { flags: 'a' });


var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));



//CORS
var cors = require('cors');
// app.use(cors());
app.use('/api', cors());



var colors = require('colors');
app.use("*", function (req, res, next) {

    var arg = url.parse(req.url).query;
    var params = querystring.parse(arg);


    var log = {
        method: req.method,
        url: req.originalUrl,
        body: req.body,
        params: params,

    };
    console.log(colors.blue('###', JSON.stringify(log, null, 2)));
    next();

});

// app.use(logger('dev'));     //打印到控制台  
app.use(logger('combined', { stream: accessLogStream }));      //打印到log日志  

app.use(require('cookie-parser')(secret.salt))

const session = require('express-session')
app.use(session({
    secret: 'zhouABC'
    // resave: true,
    // aveUninitialized: true
}))


var bodyParser = require("body-parser");
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


var ejs = require('ejs')
app.use('/public', express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/upload'));
app.use(express.static(__dirname + '/uploads'));
app.use('/views', express.static(__dirname + '/views'));
app.set('view engine', 'html')
app.engine('html', ejs.__express);
app.use(function (req, res, next) {

    // console.log('****');
    // for(var name in req.headers){
    //     console.log(name,'=',req.headers[name]);
    // }
    next();
    // console.log('####');
})
app.get('/', function (req, res) {

    req.session.userName = 'zhou';
    // res.cookie('zhou','nom nom',{httpOnly: true});
    // res.cookie('wng',12333);
    // res.cookie('li','123456789')
    return res.redirect(303, '/views/noteItemHome.html');

});

app.use('/files', require('./routes/fileRoute'));
app.use('/books', require('./routes/bookRoute'))
app.use('/dbs', require('./routes/dbRoute'));
app.use('/notes', require('./routes/noteRoute'));
app.use('/sysConfigs', require('./routes/sysConfigRoute'));
app.use('/sysSettings', require('./routes/sysSettingRoute'));
app.use('/walleCates', require('./routes/walleCateRoute'));
app.use('/walleArticles', require('./routes/walleArticleRoute'));
app.use('/enotes', require('./routes/enoteRoute'));
app.use('/edetails', require('./routes/edetailRoute'));
app.use('/cmds', require('./routes/cmdRoute'));
// app.use('/musics',require('./routes/musicRoute'));


//全局错误处理
app.use(function (err, req, res, next) {
    res.status(err.status || 500).json({
        code: -1,
        msg: 'server err'
    })
})

var conf = require('./tool/configTool');
const port = conf.sys.server.port || 9000;

app.listen(port, function () {

    console.log("App listening on port", port);
})


require('./config/mongodb')();




console.log('hello'.green); // outputs green text
console.log(colors.green('hello')); // outputs green text
// console.log(' please visit:   http://localhost:9000')
// color.GREEN.print(' please visit:   http://localhost:'+port)

console.log(colors.green(' please visit:   http://localhost:' + port)); 